import axios from 'axios'
import router from './../router'
import CONFIG from './../config'
import {DelCookie} from "../util";

const instance = axios.create({
  baseURL: CONFIG.baseUrl,
  headers:{'Authorization': 'Bearer '+CONFIG.token,},
  // timeout:10000
});

// Add a request interceptor
instance.interceptors.request.use(function (config) {
  // Do something before request is sent
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

instance.interceptors.response.use(function (response) {
  // Do something with response data
  if(response.config.url.indexOf('login') !== -1){
    instance.defaults.headers = {'Authorization': 'Bearer '+response.data.data.token};
  }
  return response.data
}, function (error) {
  // Do something with response error
  // 处理错误
  // 若token过期，重新跳转到登录页面
  try{
    if(error.response.data.status_code === 401 ){
      DelCookie('token_');
      router.push('/login')
    }
    if(error.response.data.status_code === 500 && error.response.data.msg === 'Unauthenticated.'){
      DelCookie('token_');
      router.push('/login')
    }
  }catch (e) {}
  return Promise.reject(error);
});

//login
const login =(params) =>{
  return instance.post('login',params)
};
//发送短信
const sendMsg =(params) =>{
  return instance.post('sendMsg',params)
};

// 注册
const register =(params) =>{
  return instance.post('register',params)
};

//公共
//获取七牛的token
const uploadToken = (params)=>{
  return instance.get('upload/token',{params:params})
};
//七牛获取文件临时访问URL
const uploadSignUrl = (params)=>{
  return instance.post('upload/signUrl',params)
};
//省市区
const getAreas = (params) =>{
  return instance.get('areas',{params:params})
};
//识别身份证信息
const getImageInfo = (params) =>{
  return instance.get('getImageInfo',{params:params})
};

//获取产品列表
const getProducts = (params)=>{
  return instance.get('products',params)
};

//个人中心
const getPersonal = (params)=> {
  return instance.get('user/personalCenter', params)
};
//用户贷款记录
const getUserRecord =(params)=>{
  return instance.get('loan/record',{params:params})
};
//用户逾期记录
const getUserOverdue =(params)=>{
  return instance.get('loan/overdue',{params:params})
};
//用户待支付服务费记录
const getUserWaitPay = (params)=>{
  return instance.get('loan/waitPay',{params:params})
};
//还款计划
const getLoanPayPlan= (params)=>{
  return instance.get('loan/payPlan/'+params.id,{params:params})
};

//获取个人用户详细信息
const getUserInfo = (params)=>{
  return instance.get('userInfo',{params:params})
};
//获取企业用户详细信息
const getEnterpriseInfo = (params)=>{
  return instance.get('enterpriseInfo',{params:params})
};
//完善个人用户个人信息
const postUserInfo = (params)=>{
  return instance.post('userInfo',params)
};
//完善企业用户个人信息
const postEnterpriseInfo = (params)=>{
  return instance.post('enterpriseInfo',params)
};
//编辑个人用户个人信息
const putUserInfo = (params)=>{
  return instance.put('userInfo',params)
};
//编辑企业用户个人信息
const putEnterpriseInfo = (params)=>{
  return instance.put('enterpriseInfo',params)
};
//上传个人用户身份证信息
const userIdCard = (params)=>{
  return instance.patch('userIdCard',params)
};
//上传企业用户身份证信息

const enterpriseIdCard = (params)=>{
  return instance.patch('enterpriseIdCard',params)
};
//修改密码
const securityPassword = (params)=>{
  return instance.patch('security/password',params)
};
//修改手机号码
const securityPhone = (params)=>{
  return instance.patch('security/phone',params)
};
//发送手机验证码
const getSecurityPhoneCode = (params)=>{
  return instance.get('security/phone/code',{params:params})
};
//验证手机验证码
const securityPhoneCode = (params)=>{
  return instance.post('security/phone/code',params)
};
//验证工具列表
const getSecurity = (params)=>{
  return instance.get('security',{params:params})
};
//发送邮件验证码
const getSecurityEmailCode = (params)=>{
  return instance.get('security/email/code',{params:params})
};
//验证邮件验证码
const postSecurityEmailCode = (params)=>{
  return instance.post('security/email/code',params)
};
//修改邮箱
const securityEmail = (params)=>{
  return instance.patch('security/email',params)
};

//贷款信息
//新增个人贷款信息
const addLoanUserInfo = (params)=>{
  return instance.post('loan/user',params)
};
//新增企业贷款信息
const addLoanEnterpriseInfo = (params)=>{
  return instance.post('loan/enterprise',params)
};
//贷款详细
const getUserLoanInfo = (params)=>{
  return instance.get('user/loan/'+params.id,{params:params})
};

//获取贷款资料
const getLoanData = (params)=>{
  return instance.get('loan/data/'+params.id,{params:params})
};

//上传保费分期资料
const uploadInsuranceCost = (params)=>{
  return instance.post('loan/insuranceCost',params)
};

//上传营运车小货车资料
const uploadCarBusiness = (params)=>{
  return instance.post('carBusiness/upload',params)
};
//上传家用车资料
const uploadCarPersonal = (params)=>{
  return instance.post('carPersonal/upload',params)
};
//补充文件
const uploadAddFile = (params)=>{
  return instance.patch('addFile/upload',params)
};
//获取已补充文件资料
const getAddFile = (params)=>{
  return instance.get('getAddFile/'+params.id,{params:params})
};

//财务中心
const getLoanFees = (params)=>{
  return instance.get('loan/fees',{params:params})
};
//实名认证结果查询
const getFaceHas = (params)=>{
  return instance.get('face/has',{params:params})
};


//获取所有新闻
const getArticles = (params)=>{
  return instance.get('articles',{params:params})
};
//获取新闻详细信息
const getArticleDetail= (params)=>{
  return instance.get('article/'+params.id,{params:params})
};
//获取轮播图
const getBanner = (params)=>{
  return instance.get('banner',{params:params})
};


export default {
  login,
  sendMsg,
  register,

  getAreas,
  uploadToken,
  uploadSignUrl,
  getImageInfo,

  getProducts,
  getPersonal,

  getUserRecord,
  getUserOverdue,
  getUserWaitPay,
  getLoanPayPlan,
  getLoanFees,

  getUserInfo,
  getEnterpriseInfo,
  postUserInfo,
  postEnterpriseInfo,
  putUserInfo,
  putEnterpriseInfo,
  enterpriseIdCard,
  userIdCard,
  getFaceHas,

  getSecurity,
  getSecurityPhoneCode,
  securityPhoneCode,
  securityPhone,
  getSecurityEmailCode,
  postSecurityEmailCode,
  securityEmail,
  securityPassword,

  addLoanUserInfo,
  addLoanEnterpriseInfo,
  getUserLoanInfo,

  getLoanData,

  uploadCarPersonal,
  uploadCarBusiness,
  uploadInsuranceCost,
  uploadAddFile,
  getAddFile,

  getArticles,
  getBanner,
  getArticleDetail
}

